<?php


namespace app\common\validate;


class AddressValidate extends CommonValidate
{
    protected $rule    = [
        'address_id|地址ID'       => 'require|integer|between:1,100000000',
        'name|收货人'              => 'require|length:1,45',
        'mobile_number|收货人电话号码' => 'require',
        'province|省'            => 'require|length:1,45',
        'city|城市'               => 'require|length:1,45',
        'area|区'                => 'require|length:1,45',
        'detail|详细地址'           => 'require|length:1,100',
        'is_default|默认地址状态'     => 'require|bool',
    ];
    protected $message = [];
    protected $scene   = [
        'getAddress' => [
            'address_id'
        ],
        'postAddress'   => [
            'name',
            'mobile_number',
            'province',
            'city',
            'area',
            'detail',
            'is_default',
        ],
        'putAddress'    => [
            'address_id',
            'name',
            'mobile_number',
            'province',
            'city',
            'area',
            'detail',
            'is_default',
        ],
        'deleteAddress' => ['address_id'],
    ];

    /**
     * @param $value
     * @return bool
     */
    protected function enumAddress($value): ?bool
    {
        try {
            if (false !== filter_var($value, FILTER_VALIDATE_INT)) {
                $rule = '1,1000000';
                if (is_string($rule)) {
                    $rule = explode(',', $rule);
                }
                [
                    $min,
                    $max,
                ] = $rule;

                return $value >= $min && $value <= $max;
            }
            if ($value === 'null') {
                return true;
            }
            return false;
        } catch (\Exception $e) {
            return false;
        }
    }
}